const elevator = world.querySelector('#电梯板');
if (elevator) {
  elevator.using = false;
  elevator.onEntityContact(async ({ other }) => {
    if (!other.player) {
      return;
    }
    const level_1 = await other.player.dialog({
      type: GameDialogType.SELECT,
      title: `电梯`,
      content: `请选择您要去的楼层`,
      options: ['1层', '2层', '3层', '4层', '5层', '6层'],
    });
    if (level_1) {
      let high: number, level: string;
      switch (level_1.index) {
        case 0: //-1.3
          high = 9.1;
          level = '一层';
          break;
        case 1:
          high = 19.1;
          level = '二层';
          break;
        case 2:
          high = 29.1;
          level = '三层';
          break;
        case 3:
          high = 39.1;
          level = '四层';
          break;
        case 4:
          high = 49.1;
          level = '五层';
          break;
        case 5:
          high = 59.1;
          level = '六层';
          break;
        default:
          return;
      }
      if (elevator.using && other.player) {
        other.player.directMessage(`当前电梯正在使用中，请稍候！`);
        return;
      }
      elevator.using = true;
      if (elevator.position.y >= high) {
        while (elevator.position.y > high) {
          elevator.position.y -= 0.005;
          await sleep(1);
        }
      } else {
        while (elevator.position.y < high) {
          elevator.position.y += 0.005;
          await sleep(1);
        }
      }
      elevator.using = false;
      const e = world.querySelector(`#${level}`);
      if (!e) {
        other.player.directMessage(`未找到${level}的电梯门，请联系管理员！`);
        return;
      }
      for (let i = 0; i <= 100; i++) {
        e.position.z += 0.03;
      }
      await sleep(10000);
      for (let i = 0; i <= 100; i++) {
        e.position.z -= 0.03;
      }
    }
  });
}
world.querySelectorAll('.电梯门').forEach(async (e) => {
  e.enableInteract = true;
  e.interactRadius = 6;
  e.interactHint = '电梯';
  e.onInteract(async ({ entity }) => {
    if (!elevator) {
      return;
    }
    if (!elevator.using && !e.player) {
      if (elevator.using) {
        entity.player.directMessage(`当前电梯正在使用中，请稍候！`);
        return;
      }
      elevator.using = true;
      const high1 = e.position.y - 1.3;
      if (elevator.position.y > high1) {
        while (elevator.position.y > high1) {
          elevator.position.y -= 0.005;
          await sleep(1);
        }
      } else if (elevator.position.y < high1) {
        while (elevator.position.y < high1) {
          elevator.position.y += 0.005;
          await sleep(1);
        }
      }
      for (let i = 0; i <= 100; i++) {
        e.position.z += 0.03;
      }
      elevator.using = false;
      await sleep(10000);
      for (let i = 0; i <= 100; i++) {
        e.position.z -= 0.03;
      }
    }
  });
});
